//
//  QOilTabBarController.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/7/8.
//  Copyright © 2017年 杨凯. All rights reserved.
//
#define TabbarHeight     ([[UIApplication sharedApplication] statusBarFrame].size.height>20?34:0) // 适配iPhone x 底栏高度
#import "QOilTabBarController.h"
//#import "QMainVC.h"
#import "NewMainVC.h"
#import "QMySelfVC.h"
//#import "NewMySelfVC.h"
#import "QMessageVC.h"
#import "QNavigationVC.h"
#import "QLoginVC.h"
#import "QAllOrderMoreVC.h"
@interface QOilTabBarController ()<UITabBarControllerDelegate>

@property (nonatomic, strong) NSArray *tabBarItems;

@end

@implementation QOilTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.viewControllers = self.tabBarItems;
    self.delegate = self;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(PopToMainVCNotification) name:@"PopToMainVCNotification" object:nil];
}


- (void)PopToMainVCNotification{
    
    self.selectedIndex  = 0;
}
- (void)dealloc{
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
    
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    if ([viewController.tabBarItem.title isEqualToString:@"订单"]) {
        if ([QUserInfo sharedInfo].loginModel) {
            
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTabBar2 object:nil];
            
            //return YES;
        }else{
            QLoginVC * vc = [[QLoginVC alloc] init];
            [(QNavigationVC *)tabBarController.selectedViewController pushViewController:vc animated:YES];
           // return NO;
        }
    }
    
    if (tabBarController.selectedIndex == 0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTabBar1 object:nil];
    }else if (tabBarController.selectedIndex == 2){
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTabBar3 object:nil];
        
    }else if (tabBarController.selectedIndex == 3){
        
        if (viewController == self.viewControllers[3]) {
            //点击中间tabbarItem，不切换，让当前页面跳转
            //return NO;
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTabBar4 object:nil];
            
        }
        
    }
   // return YES;
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    return YES;
}

#pragma mark - Getter/Setter
- (NSArray*)tabBarItems {
    if (!_tabBarItems) {
//        QMainVC * vc1 = [[QMainVC alloc] init];
//        vc1.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:[[UIImage imageNamed:@"tabbar_main"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tabbar_main_selected"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
//        QNavigationVC *nav1 = [[QNavigationVC alloc]initWithRootViewController:vc1];

        NewMainVC * vc1 = [[NewMainVC alloc] init];
        vc1.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:[[UIImage imageNamed:@"tabbar_main"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tabbar_main_selected"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
        QNavigationVC *nav1 = [[QNavigationVC alloc]initWithRootViewController:vc1];
        
        QAllOrderMoreVC * vc2 = [[QAllOrderMoreVC alloc] init];
        vc2.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"订单" image:[[UIImage imageNamed:@"tabbar_order"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tabbar_order_selected"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
        QNavigationVC *nav2 = [[QNavigationVC alloc]initWithRootViewController:vc2];

        
        QMessageVC *vc3 = [[QMessageVC alloc]init];
        vc3.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"消息" image:[[UIImage imageNamed:@"tabbar_message"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tabbar_message_selected"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
        QNavigationVC *nav3 = [[QNavigationVC alloc]initWithRootViewController:vc3];
        
        
       
        QMySelfVC *vc4 = [[QMySelfVC alloc] init];
        vc4.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[[UIImage imageNamed:@"tabbar_self"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tabbar_self_selected"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
        QNavigationVC *nav4 = [[QNavigationVC alloc]initWithRootViewController:vc4];
        
//        NewMySelfVC *vc4 = [[NewMySelfVC alloc] init];
//        vc4.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[[UIImage imageNamed:@"tabbar_self"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tabbar_self_selected"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
//                QNavigationVC *nav4 = [[QNavigationVC alloc]initWithRootViewController:vc4];
        
        _tabBarItems = [[NSArray alloc]initWithObjects:nav1,nav2, nav3,nav4, nil];
    }
    return _tabBarItems;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
